//
//  playerHandTest().c
//  
//
//  Created by Preston Skaggs on 3/18/14.
//  This tests various function calls that affect the players hand.
//  This test needs a lot of work due to the nature of the playCard call.  I was hoping my partner would complete that test case so we could incorporate some of the code here.
//

#include <stdio.h>
#include <stdlib.h>
#include "dominion.h"
#include "rngs.h"
#include "dominion_helpers.h"
#include <assert.h>
#include <time.h>
#include <math.h>
#include <string.h>

#define NUM_TESTS 1000
#define NUM_K_CARDS 10
#define NUM_CARD_TYPES (treasure_map+1)
#define TRUE 1
#define FALSE 0
#define MIN_PLAYERS 2

/*************************************
 Supporting Functions
 ************************************/

/*------------------------------------
 Randomly Select Kingdom Cards
 -----------------------------------*/
//This is taken from interface.c
void selectKingdomCards(int randomSeed, int kingCards[NUM_K_CARDS]) {
    int i, used, card, numSelected = 0;
    SelectStream(1);
	PutSeed((long)randomSeed);
    
	
    while(numSelected < NUM_K_CARDS) {
        used = FALSE;
        card = floor(Random() * NUM_CARD_TYPES);
        if(card < adventurer) continue;
        for(i = 0; i < numSelected; i++) {
            if(kingCards[i] == card) {
                used = TRUE;
                break;
            }
        }
        if(used == TRUE) continue;
        kingCards[numSelected] = card;
        numSelected++;
    }
}

void printState(struct gameState *game) {
    FILE *fd = fopen("./testHand.out", "a");
    if(fd == NULL){
        perror("Can't open file");
    }
    int numActions = game->numActions;
    int numCoins = game->coins;
    int numBuys = game->numBuys;
    int currentPlayer = game->whoseTurn;
    int handCount = game->handCount[currentPlayer];
    int discard = game->discardCount[currentPlayer];
    int deckCount = game->deckCount[currentPlayer];
    //int phase = game->phase;
    fprintf(fd,"Player %d:\n%d actions\n%d coins\n%d buys\n %d handCount\n%d discardCount\n%d deckCount\n\n", currentPlayer, numActions, numCoins, numBuys, handCount, discard, deckCount);
    fclose(fd);
}

/*------------------------------------
 RNG 0-1
 -----------------------------------*/
double randGen(){
    return  (double)rand()/(double)RAND_MAX;
}

int main(){
    struct gameState *state1 = malloc(sizeof(struct gameState));
    struct gameState *state2 = malloc(sizeof(struct gameState));
    int kingdom[NUM_K_CARDS];
    int i, test, supplyPos, choice1, choice2, choice3, card, count, selector;
    srand(time(NULL));
    selectKingdomCards(rand(), kingdom);
    initializeGame(2, kingdom, rand(), state1);
    int player = 0;
    state1->whoseTurn = player;
    memcpy(state2, state1, sizeof(struct gameState));
    count = state1->handCount[player];
    for(i=0; i<5; i++){
        selector = floor(randGen()*4);
        test = selector;
        switch(test){
            case 0:{
                printf("Drawing a card\n");
                if(drawCard(player, state1) == -1){
                    printf("Unable to draw a card\n");
                    printState(state1);
                    break;
                };
                if(state2->deckCount[player] > 0){
                    state2->hand[player][state2->handCount[player]] = state1->hand[player][state2->handCount[player]];
                    state2->handCount[player]++;
                    state2->deckCount[player]--;
                }
                break;
                    
            }
            case 1:{
                printf("Buying a card\n");
                selector = floor(randGen() *10);
                card = kingdom[selector];
                supplyPos = card;
                if(buyCard(supplyPos, state1) == -1){
                    printf("Unable to buy card\n");
                    printState(state1);
                    break;
                }
                break;
            }
            case 2:{
                printf("Discarding a card\n");
                selector = floor(randGen()*(state1->handCount[player]-1));
                supplyPos = state1->hand[player][selector];
                if(discardCard(selector, player, state1, 2)== -1){
                    printf("Unable to discard\n");
                    printState(state1);
                    break;
                }
                break;
            }
            case 3:{
                //This case needs a lot of refining.  Bad choices will likely be made a lot.
                printf("Playing a card\n");
                //randomly select a card to play from the kingdom and add it to players hand
                selector =floor(randGen() *10);
                card = kingdom[selector];
                state1->hand[player][state1->handCount[player]] = card;
                state1->handCount[player]++;
                choice1 = floor(randGen()*NUM_CARD_TYPES);
                choice2 = floor(randGen()*NUM_CARD_TYPES);
                choice3 = floor(randGen()*NUM_CARD_TYPES);
                if(playCard(state1->handCount[player]-1, choice1, choice2, choice3, state1)==-1){
                    printf("Failed to play card %d\n", card);
                    printState(state1);
                    break;
                }
                break;
            }
               
            default: break;
        }
        
    }
}
